//
//  RootBarViewController.m
//  随拍
//
//  Created by lanou3g on 16/1/18.
//  Copyright © 2016年 Ashoka. All rights reserved.
//

#import "RootBarViewController.h"
#import "RootViewController.h"
#import "HomeTableViewController.h"
#import "MineTableViewController.h"
#import "PisctureViewController.h"
#import "PatViewController.h"
#import "DisCoverViewController.h"
#import "SPTabBar.h"
#import "ComposeViewController.h"
#import "TPCSpringMenu.h"

@interface RootBarViewController () <SPTabBarDelegate,TPCSpringMenuDataSource,TPCSpringMenuDelegate>
@property (weak, nonatomic) TPCSpringMenu *menu;
@property (nonatomic ,strong) ComposeViewController *comVC;
@end

@implementation RootBarViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    HomeTableViewController *homeVC = [[HomeTableViewController alloc] init];
    [self addChildViewController:homeVC title:@"首页" image:@"" selected:@""];
    
    
    
    PatViewController *withVC = [[PatViewController alloc] init];
    [self addChildViewController:withVC title:@"随拍" image:@"" selected:@""];
    
//    PisctureViewController *pisVC = [[PisctureViewController alloc] init];
//    [self addChildViewController:pisVC title:@"照相" image:@"" selected:@""];
    
    DisCoverViewController *foundVC = [[DisCoverViewController alloc] init];
    [self addChildViewController:foundVC title:@"发现" image:@"" selected:@""];
    
    
    MineTableViewController *userVC = [[MineTableViewController alloc] init];
    [self addChildViewController:userVC title:@"我的" image:@"" selected:@""];
    
    
    SPTabBar *barBnt = [[SPTabBar alloc] init];
    [self setValue:barBnt forKeyPath:@"tabBar"];
    
    
    TPCItem *item1 = [TPCItem itemWithImage:[UIImage imageNamed:@"tabbar_compose_camera"] title:@"相机"];
    TPCItem *item2 = [TPCItem itemWithImage:[UIImage imageNamed:@"tabbar_compose_idea"] title:@"文字"];
    TPCItem *item3 = [TPCItem itemWithImage:[UIImage imageNamed:@"tabbar_compose_lbs"] title:@"签到"];
    TPCItem *item4 = [TPCItem itemWithImage:[UIImage imageNamed:@"tabbar_compose_more"] title:@"更多"];
    
    TPCItem *item5 = [TPCItem itemWithImage:[UIImage imageNamed:@"tabbar_compose_photo"] title:@"相册"];
    TPCItem *item6 = [TPCItem itemWithImage:[UIImage imageNamed:@"tabbar_compose_review"] title:@"点评"];
    NSArray *items = @[item1, item2, item3, item4, item5, item6];
    
    
    TPCSpringMenu *menu = [TPCSpringMenu menuWithItems:items];
    // 按钮文字颜色
    menu.buttonTitleColor = [UIColor blackColor];
    // 按钮行数
    menu.columns = 3;
    // 最后一个按钮与底部的距离
    menu.spaceToBottom = 100;
    // 按钮半径（只支持圆形图片，非圆形图片以宽度算）
    menu.buttonDiameter = 50;
    // 允许点击隐藏menu
    menu.enableTouchResignActive = YES;
    menu.dataSource = self;
    menu.delegate = self;
    [self.view addSubview:menu];
    _menu = menu;

}

/**
 *  代码简化
 *
 *  @param childController TableView
 *  @param title           标题
 *  @param image           图标
 *  @param selected        选中时的图标
 */
- (void)addChildViewController:(UIViewController *)childController title:(NSString *)title image:(NSString *)image selected:(NSString *)selected{
    
    childController.title = title;
    
    childController.tabBarItem.image = [UIImage imageNamed:image];
    childController.tabBarItem.selectedImage = [[UIImage imageNamed:selected] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    UITabBarController *tabBar = [[UITabBarController alloc] init];
    
    RootViewController *naVC = [[RootViewController alloc] initWithRootViewController:childController];
    [tabBar addChildViewController:naVC];
    [self addChildViewController:naVC];
    
}

#pragma mark SPTabBarDelegate代理方法
- (void)tabBarDidClickPlusButton:(SPTabBar *)tabBar
{
    [_menu becomeActive];
}


#pragma mark TPCSpringMenuDataSource
- (UIButton *)buttonToChangeActiveForSpringMenu:(TPCSpringMenu *)menu
{
    UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
    btn.frame = CGRectMake(0, 0, self.view.bounds.size.width, 40);
    btn.backgroundColor  = [UIColor whiteColor];
    [btn setImage:[UIImage imageNamed:@"tabbar_compose_background_icon_close"] forState:UIControlStateNormal];
    
    return btn;
}

- (UIView *)backgroundViewOfSpringMenu:(TPCSpringMenu *)menu
{
    UIView *view = [[UIView alloc] initWithFrame:self.view.bounds];
    view.backgroundColor = [UIColor whiteColor];
    UIImageView *imageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"compose_slogan"]];
    imageView.bounds = CGRectMake(0, 0, 154, 48);
    imageView.center = CGPointMake(self.view.bounds.size.width * 0.5, 100);
    [view addSubview:imageView];
    
    return view;
}
#pragma mark TPCSpringMenuDelegate
- (void)springMenu:(TPCSpringMenu *)menu didClickBottomActiveButton:(UIButton *)button
{
    
}

- (void)springMenu:(TPCSpringMenu *)menu didClickButtonWithIndex:(NSInteger)index
{
    switch (index) {
        case 0:
            NSLog(@"1");
            break;
            
        case 1:
            self.comVC = [[ComposeViewController alloc] init];
            UINavigationController *naVC = [[UINavigationController alloc] initWithRootViewController:_comVC];
            [self presentViewController:naVC animated:YES completion:nil];
            
            [self.menu resignActive];
            break;
            
    }
}




@end
